package com.atguigu01.collection;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CirclePointsDemo2 extends JPanel {
    private static final int RADIUS = 300; // 设置圆的半径
    private static final int DIVISIONS = 12; // 设置等分数量

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g; // 将Graphics对象转换为Graphics2D对象
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 开启抗锯齿
        int centerX = getWidth() / 2; // 获取面板中心横坐标
        int centerY = getHeight() / 2; // 获取面板中心纵坐标
        int diameter = 2 * RADIUS; // 计算圆的直径
        int x = centerX - RADIUS; // 计算圆的左上角横坐标
        int y = centerY - RADIUS; // 计算圆的左上角纵坐标
        g2d.drawOval(x, y, diameter, diameter); // 绘制圆
        for (int i = 0; i < DIVISIONS; i++) {
            double angle1 = 2 * Math.PI * i / DIVISIONS; // 计算扇形的起始角度
            double angle2 = 2 * Math.PI * (i + 1) / DIVISIONS; // 计算扇形的终止角度
            int x1 = centerX + (int) (RADIUS * Math.cos(angle1)); // 计算扇形起始点横坐标
            int y1 = centerY + (int) (RADIUS * Math.sin(angle1)); // 计算扇形起始点纵坐标
            int x2 = centerX + (int) (RADIUS * Math.cos(angle2)); // 计算扇形终止点横坐标
            int y2 = centerY + (int) (RADIUS * Math.sin(angle2)); // 计算扇形终止点纵坐标
            g2d.drawLine(centerX, centerY, x1, y1); // 绘制分割线
        }

        int smallRadius1 = 60; // 设置第一个小圆的半径
        int smallX1 = centerX - smallRadius1; // 计算第一个小圆的左上角横坐标
        int smallY1 = centerY - smallRadius1; // 计算第一个小圆的左上角纵坐标
        g2d.drawOval(smallX1, smallY1, smallRadius1 * 2, smallRadius1 * 2); // 绘制第一个小圆

        int smallRadius2 = 120; // 设置第二个小圆的半径
        int smallX2 = centerX - smallRadius2; // 计算第二个小圆的左上角横坐标
        int smallY2 = centerY - smallRadius2; // 计算第二个小圆的左上角纵坐标
        g2d.drawOval(smallX2, smallY2, smallRadius2 * 2, smallRadius2 * 2); // 绘制第二个小圆

        int smallRadius3 = 180; // 设置第三个小圆的半径
        int smallX3 = centerX - smallRadius3; // 计算第三个小圆的左上角横坐标
        int smallY3 = centerY - smallRadius3; // 计算第三个小圆的左上角纵坐标
        g2d.drawOval(smallX3, smallY3, smallRadius3 * 2, smallRadius3 * 2); // 绘制第三个小圆

        int smallRadius4 = 240; // 设置第四个小圆的半径
        int smallX4 = centerX - smallRadius4; // 计算第四个小圆的左上角横坐标
        int smallY4 = centerY - smallRadius4; // 计算第四个小圆的左上角纵坐标
        g2d.drawOval(smallX4, smallY4, smallRadius4 * 2, smallRadius4 * 2); // 绘制第四个小圆

        int pointCount = 1000; // 设置点的数量
        double[] angles = new double[pointCount]; // 存储每个点的角度
        for (int i = 0; i < pointCount; i++) {
            angles[i] = 2 * Math.PI * i / pointCount; // 计算每个点的角度
        }
        int pointRadius = 2; // 设置点的半径
        for (int i = 0; i < pointCount; i++) {
            int x1 = centerX + (int) (RADIUS * Math.cos(angles[i])); // 计算点的横坐标
            int y1 = centerY + (int) (RADIUS * Math.sin(angles[i])); // 计算点的纵坐标
            g2d.fillOval(x1 - pointRadius, y1 - pointRadius, pointRadius * 2, pointRadius * 2); // 绘制点
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Circle Divide");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CirclePointsDemo2 panel = new CirclePointsDemo2();
        frame.add(panel);
        frame.setVisible(true);
    }
}
